package net.tv90.bfmccore.util.area;

import net.tv90.bfmccore.util.vector.Vector2i;
import net.tv90.bfmccore.util.vector.Vector3i;

import java.util.List;

public class Area3i extends Area2i{

    public int maxHeight;
    public int minHeight;

    public Area3i(List<Vector2i> vectorList, int minHeight, int maxHeight) {
        super(vectorList);
        this.maxHeight = maxHeight;
        this.minHeight = minHeight;
    }


    /**
     * 判断点是否位于区域内
     * @param point 点
     * @return true or false
     */
    @Override
    public boolean isInArea(Vector3i point){
        if(point == null || vectorList == null) return false;
        if(point.y > maxHeight || point.y < minHeight){
            return false;
        }
        int N = vectorList.size();
        boolean boundOrVertex = true; //如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
        int intersectCount = 0;//cross points count of x
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        Vector2i p1, p2;//neighbour bound vertices

        p1 = vectorList.get(0);//left vertex
        for(int i = 1; i <= N; ++i){//check all rays
            if(point.reduction().equals(p1)){
                return boundOrVertex;//p is an vertex
            }

            p2 = vectorList.get(i % N);//right vertex
            if(point.x < Math.min(p1.x, p2.x) || point.x > Math.max(p1.x, p2.x)){//ray is outside of our interests
                p1 = p2;
                continue;//next ray left point
            }

            if(point.x > Math.min(p1.x, p2.x) && point.x < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of)
                if(point.z <= Math.max(p1.y, p2.y)){//x is before of ray
                    if(p1.x == p2.x && point.z >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray
                        return boundOrVertex;
                    }

                    if(p1.y == p2.y){//ray is vertical
                        if(p1.y == point.z){//overlies on a vertical ray
                            return boundOrVertex;
                        }else{//before ray
                            ++intersectCount;
                        }
                    }else{//cross point on the left side
                        double xinters = (point.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y
                        if(Math.abs(point.z - xinters) < precision){//overlies on a ray
                            return boundOrVertex;
                        }
                        if(point.z < xinters){//before ray
                            ++intersectCount;
                        }
                    }
                }
            }else{//special case when ray is crossing through the vertex
                if(point.x == p2.x && point.z <= p2.y){//p crossing over p2
                    Vector2i p3 = vectorList.get((i+1) % N); //next vertex
                    if(point.x >= Math.min(p1.x, p3.x) && point.x <= Math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x
                        ++intersectCount;
                    }else{
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;//next ray left point
        }

        //偶数在多边形外，奇数在多边形内
        return intersectCount % 2 != 0;
    }



    public boolean isInArea2(Vector3i point) {
        int intersectionCount = 0;
        List<Vector2i> vectors = getVectorList();
        for (int i = 0; i < vectors.size(); i++) {
            Vector2i current = vectors.get(i);
            Vector2i next = vectors.get((i + 1) % vectors.size());
            int side = (next.getX() - current.getX()) * (point.getY() - current.getY())
                    - (next.getY() - current.getY()) * (point.getX() - current.getX());
            if (side == 0) {
                int dotProduct = (point.getX() - current.getX()) * (next.getX() - current.getX())
                        + (point.getY() - current.getY()) * (next.getY() - current.getY());
                if (dotProduct >= 0 && dotProduct <= (next.getX() - current.getX()) * (next.getX() - current.getX())
                        + (next.getY() - current.getY()) * (next.getY() - current.getY())) {
                    return true;
                }
            } else if (side > 0 && current.getY() <= point.getY() && next.getY() > point.getY()) {
                intersectionCount++;
            } else if (side < 0 && next.getY() <= point.getY() && current.getY() > point.getY()) {
                intersectionCount++;
            }
        }
        return intersectionCount % 2 != 0;
    }
}
